package com.ruoyi.project.product.series.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.product.series.domain.Series;
import com.ruoyi.project.product.series.service.SeriesService;
import com.ruoyi.project.system.post.domain.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: 高柯
 * @Date: 2020/4/15
 * @Description: com.ruoyi.project.product.series.controller
 * @version: 1.0
 */
@Controller
@RequestMapping("/product/series")
public class SeriesController extends BaseController {

    @Autowired
    private SeriesService seriesService;

    //声明资源地址的公共前缀
    private String prefix = "product/series";

    //声明单元方法:跳转产品系列页面
    @RequiresPermissions("product:series:view")
    @GetMapping()
    public String series(){
        return prefix+"/series";
    }

    @RequiresPermissions("product:series:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Series series)
    {
        startPage();
        List<Series> list = seriesService.selectSeriesList(series);
        return getDataTable(list);
    }

    /**
     * 新增产品系列
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品系列
     */
    @RequiresPermissions("product:series:add")
    @Log(title = "产品系列管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Series series)
    {
        if (UserConstants.SERIES_NAME_NOT_UNIQUE.equals(seriesService.checkSeriesNameUnique(series)))
        {
            return error("新增产品系列'" + series.getSeriesName() + "'失败，产品系列名称已存在");
        }
        return toAjax(seriesService.insertSeries(series));
    }

    /**
     * 校验产品系列名称
     */
    @PostMapping("/checkSeriesNameUnique")
    @ResponseBody
    public String checkSeriesNameUnique(Series series)
    {
        return seriesService.checkSeriesNameUnique(series);
    }

    /**
     * 修改产品系列
     */
    @GetMapping("/edit/{seriesId}")
    public String edit(@PathVariable("seriesId") Long seriesId, ModelMap mmap)
    {
        mmap.put("series", seriesService.selectSeriesById(seriesId));
        return prefix + "/edit";
    }

    /**
     * 修改保存产品系列
     */
    @RequiresPermissions("product:series:edit")
    @Log(title = "产品系列管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Series series)
    {
        if (UserConstants.SERIES_NAME_NOT_UNIQUE.equals(seriesService.checkSeriesNameUnique(series)))
        {
            return error("修改产品系列'" + series.getSeriesName() + "'失败，产品系列名称已存在");
        }
        return toAjax(seriesService.updateSeries(series));
    }

    /**
     * 删除产品系列
     */
    @RequiresPermissions("product:series:remove")
    @Log(title = "产品系列管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(seriesService.deleteSeriesByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

}
